home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / database / msgobj10.zip / MSGOBJMS.CPP < prev    next >
C/C++ Source or Header  |  1993-03-17  |  12KB  |  456 lines

  1. // MSGOBJMS.CPP: module to handle messages
  2.  
  3. #include <ctype.h>
  4. #include <dos.h>
  5. #include <io.h>
  6. #include <iostream.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #ifdef __WINDOWS__
  11. #include <windows.h>
  12. #endif
  13. #include "config.h"
  14. #include "msgobj.h"
  15.  
  16.  
  17. // Public functions
  18.  
  19. int message_frame::post(void)
  20. {
  21.   lasterror=MSGERR_NOERR;
  22.   if(!text)
  23.     {
  24.       lasterror=MSGERR_ENOBUFFER;
  25.       return -1;
  26.     }
  27.   if(open_area()) return -1;
  28.   switch(AREATYPE(area))
  29.     {
  30.       case 0:
  31.         if(pip_msg_post(text,from,to,subject,attributes,fromaddr,toaddr,-1,TEARLINE,ORIGIN(area)))
  32.           return -1;
  33.         break;
  34.       default:
  35.         lasterror=MSGERR_NOTIMPL;
  36.         return -1;
  37.     }
  38.   current_msg=msg_number;
  39.   return 0;
  40. }
  41.  
  42. int message_frame::modify(void)
  43. {
  44.   lasterror=MSGERR_NOERR;
  45.   if(!text)
  46.     {
  47.       lasterror=MSGERR_ENOBUFFER;
  48.       return -1;
  49.     }
  50.   if(open_area()) return -1;
  51.   if(msg_number<0 || msg_number>this->highest_message())
  52.     {
  53.       lasterror=MSGERR_ENOMSG;
  54.       return -1;
  55.     }
  56.   switch(AREATYPE(area))
  57.     {
  58.       case 0:
  59.         if(pip_msg_post(text,from,to,subject,attributes,fromaddr,toaddr,msg_number,TEARLINE,ORIGIN(area)))
  60.           return -1;
  61.         break;
  62.       default:
  63.         lasterror=MSGERR_NOTIMPL;
  64.         return -1;
  65.     }
  66.   current_msg=msg_number;
  67.   return 0;
  68. }
  69.  
  70. int message_frame::del(void)
  71. {
  72.   MSGPTR hdr;
  73.   lasterror=MSGERR_NOERR;
  74.   if(msg_number<0 || msg_number>this->highest_message())
  75.     {
  76.       lasterror=MSGERR_ENOMSG;
  77.       return -1;
  78.     }
  79.   if(area==-1)
  80.     {
  81.       lasterror=MSGERR_ENOMSG;
  82.       return -1;
  83.     }
  84.   if(open_area()) return -1;
  85.   switch(AREATYPE(area))
  86.     {
  87.       case 0:
  88.         lseek(fileptr,sizeof(hdr)*msg_number,SEEK_SET);
  89.         ::read(fileptr,&hdr,sizeof(hdr));
  90.         hdr.status|=SET_MPTR_DEL;
  91.         lseek(fileptr,sizeof(hdr)*msg_number,SEEK_SET);
  92.         if (write(fileptr,&hdr,sizeof(hdr))==-1)
  93.           {
  94.             lasterror=MSGERR_ENOWRITE;
  95.             return -1;
  96.           }
  97.         break;
  98.       default:
  99.         lasterror=MSGERR_NOTIMPL;
  100.         return -1;
  101.     }
  102.   return 0;
  103. }
  104.  
  105. int message_frame::read(int forced)
  106. {
  107.   char dstring[25],huge*z,huge*z2;
  108.   lasterror=MSGERR_NOERR;
  109.   if(msg_number<0 || msg_number>this->highest_message())
  110.     {
  111.       lasterror=MSGERR_ENOMSG;
  112.       return -1;
  113.     }
  114.   if(area==-1)
  115.     {
  116.       lasterror=MSGERR_ENOMSG;
  117.       return -1;
  118.     }
  119.   if(open_area())
  120.     return -1;
  121.   if(current_msg==msg_number && !forced) return 0;
  122.   #ifdef DEBUG
  123.     cout << "Reading message " << msg_number << "\n";
  124.   #endif
  125.   switch(AREATYPE(area))
  126.     {
  127.       case 0:
  128.         if (lseek(fileptr,sizeof(hdr)*msg_number,SEEK_SET)==-1)
  129.           {
  130.             lasterror=MSGERR_ENOSEEK;
  131.             return -1;
  132.           }
  133.         if (::read(fileptr,&hdr,sizeof(hdr))<=0)
  134.           {
  135.             lasterror=MSGERR_ENOREAD;
  136.             return -1;
  137.           }
  138.         lseek(filepkt,hdr.pos,SEEK_SET); 
  139.         ::read(filepkt,&mpkt,sizeof(mpkt));
  140.         read0((unsigned char*)dstring,filepkt,20,1);
  141.         date.parse(dstring);
  142.         read0((unsigned char*)to,filepkt,36,1);
  143.         read0((unsigned char*)from,filepkt,36,1);
  144.         read0((unsigned char*)subject,filepkt,72,1);
  145.         status=hdr.status;
  146.         attributes=mpkt.attribute;
  147.         previous=hdr.prev;
  148.         next=hdr.next;
  149.         current_msg=msg_number;
  150.         cost=times=0; // NOT IMPLEMENTED !!!!!!!!!!!!!!!!!!!!
  151.         if(text)
  152.           {
  153.             switch(mpkt.pktype)
  154.               {
  155.                 case 2:
  156.                 read0((unsigned char*)text,filepkt,textsize-256,1);
  157.                 break;
  158.               case 10:
  159.                 unpipfile((unsigned char*)text,filepkt);
  160.                 break;
  161.               default:
  162.                 return -1;
  163.               }
  164.             if(*AREATAG(area)=='#')
  165.               {
  166.                 if(stricmp(AREATAG(area),"#NETMAIL"))
  167.                   {
  168.                     fromaddr.zone=toaddr.zone=MYZONE;
  169.                     fromaddr.net=toaddr.net=MYNET;
  170.                     fromaddr.node=toaddr.node=MYNODE;
  171.                     fromaddr.point=toaddr.point=MYPOINT;
  172.                     strcpy(fromaddr.domain,MYDOMAIN);
  173.                     strcpy(toaddr.domain,MYDOMAIN);
  174.                     strcpy(fromaddr.uucp,"");
  175.                     strcpy(toaddr.uucp,"");
  176.                   }
  177.                 else
  178.                   {
  179.                     fromaddr.net=mpkt.fromnet;
  180.                     fromaddr.node=mpkt.fromnode;
  181.                     toaddr.net=mpkt.tonet;
  182.                     toaddr.node=mpkt.tonode;
  183.                     strcpy(fromaddr.domain,MYDOMAIN);
  184.                     strcpy(toaddr.domain,MYDOMAIN);
  185.                     strcpy(fromaddr.uucp,"");
  186.                     strcpy(toaddr.uucp,"");
  187.                     if((z=strstr(text,"\1TOPT "))!=NULL)
  188.                       toaddr.point=atoi(z+6);
  189.                     else
  190.                       toaddr.point=0;
  191.                     if((z=strstr(text,"\1FMPT "))!=NULL)
  192.                       fromaddr.point=atoi(z+6);
  193.                     else
  194.                       fromaddr.point=0;
  195.                     if((z=strstr(text,"\1INTL "))!=NULL)
  196.                       {
  197.                         fromaddr.zone=atoi(strstr(z+6," ")+1);
  198.                         toaddr.zone=atoi(z+6);
  199.                       }
  200.                     else
  201.                       fromaddr.zone=toaddr.zone=MYZONE;
  202.                     if(!stricmp(from,"UUCP"))
  203.                       fromaddr.parse_uucp(text,from,"From: ");
  204.                     if(!stricmp(to,"UUCP"))
  205.                       toaddr.parse_uucp(text,to,"To: ");
  206.                   }
  207.               }
  208.             else
  209.               {
  210.                 fromaddr.zone=toaddr.zone=MYZONE;
  211.                 fromaddr.net=toaddr.net=MYNET;
  212.                 fromaddr.node=toaddr.node=MYNODE;
  213.                 fromaddr.point=toaddr.point=MYPOINT;
  214.                 strcpy(fromaddr.domain,MYDOMAIN);
  215.                 strcpy(toaddr.domain,MYDOMAIN);
  216.                 strcpy(fromaddr.uucp,"");
  217.                 strcpy(toaddr.uucp,"");
  218.                 if((z=strchr(text,'('))!=NULL)
  219.                   {
  220.                     while((z2=strchr(z,'('))!=NULL)
  221.                       z=z2+1;
  222.                     z2=z;
  223.                     while(!isdigit(*z) && (*z!='@')) z++;
  224.                     if(isdigit(*z))
  225.                       fromaddr.parse(z);
  226.                   }
  227.               }
  228.           }  
  229.         break;
  230.       default:
  231.         lasterror=MSGERR_NOTIMPL;
  232.         return -1;
  233.     }
  234.   #ifdef DEBUG
  235.     cout << "Message read\n";
  236.   #endif
  237.   return 0;
  238. }
  239.  
  240. int message_frame::allocate_text(long l)
  241. {
  242.   lasterror=MSGERR_NOERR;
  243.   if(text) return 0;
  244.   #ifdef __WINDOWS__
  245.     {
  246.       textSelector=GlobalAlloc(GMEM_FIXED,l);
  247.       if(textSelector==NULL)
  248.         text=NULL;
  249.       else
  250.         text=GlobalLock(textSelector);
  251.     }
  252.   #else
  253.     text=(char huge*)malloc(l);
  254.   #endif
  255.   if(text)
  256.     {
  257.       textsize=l;
  258.       return 0;
  259.     }
  260.   textsize=0;
  261.   lasterror=MSGERR_ENOMEM;
  262.   return -1;
  263. }
  264.  
  265. int message_frame::deallocate_text(void)
  266. {
  267.   lasterror=MSGERR_NOERR;
  268.   if(!text)
  269.     {
  270.       lasterror=MSGERR_ENOBUFFER;
  271.       return -1;
  272.     }
  273.   #ifdef __WINDOWS__
  274.     GlobalUnlock(textSelector);
  275.     GlobalFree(textSelector);
  276.   #else
  277.     free(text);
  278.   #endif
  279.   textsize=0;
  280.   return 0;
  281. }
  282.  
  283. int message_frame::isolate_seen_by(AUDIT*seen_by,int&ns,int tots,AUDIT*path,int&np,int totp)
  284. {
  285.   if(open_area()) return -1;
  286.   int i;
  287.   char huge*p,huge*p2;
  288.   uint t;
  289.   memset(seen_by,0,tots*sizeof(AUDIT));
  290.   i=0; ns=0; np=0;
  291.   p2=find_origin(text);
  292.   if(p2==NULL)
  293.     {
  294.       strcat(text,TEARLINE);
  295.       strcat(text,"\r");
  296.       sprintf(fn," * Origin: Origin not found: Abort? Retry? Ignore? (%d:%d/%d.%d)\r",
  297.               MYZONE,MYNET,MYNODE,MYPOINT);
  298.       strcat(text,fn);
  299.       return 0;
  300.     }
  301.   if((p=strstr(p2,"SEEN-BY:"))!=NULL)
  302.     {
  303.       i=0;
  304.       *p=0; // elimina seen-by e path per l'export
  305.       p+=8;
  306.       if(*p==' ') p++;
  307.       while (p)
  308.         {
  309.           if (*p=='.')
  310.             {
  311.               if (i)
  312.                 {seen_by[i].net=seen_by[i-1].net; seen_by[i].node=seen_by[i-1].node;}
  313.               else
  314.                 {seen_by[i].net=MYNET; seen_by[i].node=MYNODE;}
  315.               seen_by[i].point=atoi(p+1);
  316.               ns++; i++;
  317.             }
  318.           else
  319.             if (*p>32)
  320.               {
  321.                 t=atoi(p); while (isdigit(*p)) p++;
  322.                 if (*p=='/')
  323.                   {seen_by[i].net=t; seen_by[i].node=atoi(++p); while(isdigit(*p)) p++;}
  324.                 else
  325.                   {seen_by[i].net=seen_by[i-1].net; seen_by[i].node=t; while(isdigit(*p)) p++;}
  326.                 if (*p=='.')
  327.                   seen_by[i].point=atoi(p);
  328.                 else
  329.                   seen_by[i].point=0;
  330.                 ns++; i++;
  331.               }
  332.           p=strchr(p,' '); if (p) p++;
  333.         }
  334.     }
  335.   memset(path,0,totp*sizeof(AUDIT));
  336.   i=0;
  337.   if ((p=strstr((signed char*)p2,"\1PATH:"))!=NULL)
  338.     {
  339.       p+=6;
  340.       if(*p==' ') p++;
  341.       while (p)
  342.         {
  343.           if (*p=='.')
  344.             {
  345.               if (i)
  346.                 {path[i].net=path[i-1].net; path[i].node=path[i-1].node;}
  347.               else
  348.                 {path[i].net=MYNET; path[i].node=MYNODE;}
  349.               path[i].point=atoi(p+1);
  350.             }
  351.           else
  352.             if (*p>32)
  353.               {
  354.                 t=atoi(p); while (isdigit(*p)) p++;
  355.                 if (*p=='/')
  356.                   {path[i].net=t; path[i].node=atoi(++p); while(isdigit(*p)) p++;}
  357.                 else
  358.                   {path[i].net=path[i-1].net; path[i].node=t; while(isdigit(*p)) p++;}
  359.                 if (*p=='.')
  360.                   path[i].point=atoi(p);
  361.                 else
  362.                   path[i].point=0;
  363.               }
  364.           p=strchr(p,' '); if (p) p++;
  365.           i++;
  366.         }
  367.     }
  368.   np=i;
  369.   if(!cfg.fakenet)
  370.     {
  371.       if (not_in_seen_by(seen_by,MYNET,MYNODE,MYPOINT,tots))
  372.         {
  373.           seen_by[ns].net=MYNET;
  374.           seen_by[ns].node=MYNODE;
  375.           seen_by[ns].point=MYPOINT;
  376.           ns++;
  377.         }
  378.       path[np].net=MYNET;
  379.       path[np].node=MYNODE;
  380.       path[np].point=MYPOINT;
  381.     }
  382.   else
  383.     {
  384.       if (not_in_seen_by(seen_by,MYFAKENET,MYPOINT,0,tots))
  385.         {
  386.           seen_by[ns].net=MYFAKENET;
  387.           seen_by[ns].node=MYPOINT;
  388.           seen_by[ns].point=0;
  389.           ns++;
  390.         }
  391.       path[np].net=MYFAKENET;
  392.       path[np].node=MYPOINT;
  393.       path[np].point=0;
  394.     }
  395.   np++;
  396.   return 0;
  397. }
  398.  
  399. int message_frame::highest_message(void)
  400. {
  401.   lasterror=MSGERR_NOERR;
  402.   open_area();
  403.   if(current_area==-1)
  404.     return -1;
  405.   else
  406.     switch(AREATYPE(area))
  407.       {
  408.         case 0:
  409.           return filelength(fileptr)/sizeof(MSGPTR)-1;
  410.         default:
  411.           lasterror=MSGERR_NOTIMPL;
  412.           return -1;
  413.       }
  414. }
  415.  
  416. int message_frame::total_messages(void)
  417. {
  418.   open_area();
  419.   lasterror=MSGERR_NOERR;
  420.   if(current_area==-1)
  421.     return -1;
  422.   else
  423.     switch(AREATYPE(area))
  424.       {
  425.         case 0:
  426.           return filelength(fileptr)/sizeof(MSGPTR);
  427.         default:
  428.           lasterror=MSGERR_NOTIMPL;
  429.           return -1;
  430.       }
  431. }
  432.  
  433. message_frame::message_frame(void)
  434. {
  435.   current_area=area=-1;
  436.   current_msg=msg_number=-1;
  437.   attributes=status=0;
  438.   cost=times=0;
  439.   previous=next=0;
  440.   text=NULL;
  441.   textsize=0;
  442.   lasterror=MSGERR_NOERR;
  443.   // other fields are automatically zeroed by their constructors
  444. }
  445.  
  446. message_frame::~message_frame(void)
  447. {
  448.   if(text)
  449.     #ifdef __WINDOWS__
  450.       GlobalDosFree(textSelector);
  451.     #else
  452.       free(text);
  453.     #endif
  454.   close_area();
  455. }
  456.